package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.GrantRoleDto;
import com.itheima.admin.dto.UserDto;
import com.itheima.admin.service.IUserService;
import com.itheima.admin.vo.UserPageVo;
import com.itheima.admin.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author WXL
 */
@RestController
@RequestMapping("/user")
@Api(value = "管理后台用户模块接口",tags = "admin_user",description = "用于实现后台用户的增删改查操作")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    @ApiOperation("根据用户id查询用户信息")
    public UserVo queryById(@PathVariable("id") String id){
        return userService.queryById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据用户id删除用户信息")
    public Result deleteById(@PathVariable("id") String id){
        boolean result = userService.deleteById(id);

        return new Result(result,null,result?"删除用户成功":"删除用户失败");
    }

    @PostMapping("/add")
    @ApiOperation("新增用户")
    public Result addUser(@RequestBody UserDto userDto){
        boolean result = userService.addUser(userDto);

        return new Result(result,null,result?"添加用户成功":"添加用户失败");
    }

    @PostMapping("/list")
    @ApiOperation("分页查询用户列表")
    public PageVo<UserPageVo> queryByPage(@RequestBody PageDto pageDto){

        if (pageDto.getCurrentPage() == null){
            pageDto.setCurrentPage(1);
        }
        if (pageDto.getPageSize() == null){
            pageDto.setPageSize(10);
        }

        return userService.queryByPage(pageDto);

    }

    @PutMapping("/edit")
    @ApiOperation("修改用户信息")
    public Result editUser(@RequestBody UserVo userVo){
        boolean result = userService.editUser(userVo);

        return new Result(result,null,result?"修改用户成功":"修改用户失败");
    }

    @PostMapping("/role")
    @ApiOperation("用户授权")
    public Result grantRole(@RequestBody GrantRoleDto grantRoleDto){
        boolean result = userService.grantRole(grantRoleDto);

        return new Result(result,null,result?"用户授权成功":"用户授权失败");
    }

}
